import Vue from 'vue';
import vue_router from 'vue-router';
import store from "../store";

Vue.use(vue_router);
import routes from './routes';

const $this = Vue.prototype;
import vue_state from '../store/state';
const scroll20_routes_on_leave = [/\/index/, /^\/course_info+/, /^\//];//滚动条归0的路由 , (当离开状态时候)
const router = new vue_router({
  mode: 'hash',
  base: '/',
  routes,
  scrollBehavior (to, from, savedPosition) {
    if (scroll20_routes_on_leave.some(v => v.test(from.path))) return { x: 0, y: 0 };
  },
});
router.beforeEach((to, from, next) => {
  // 设备判断
  let flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  );
  if (flag) {
    console.log("移动端打开666666666");
    // if(to.path=="/wan"){
    //   location.assign("https://zhongke3.com/h5/wanh5");
    // }else{
    //   location.assign("https://zhongke3.com/h5");
    // }
    localStorage.setItem("agennt",'Mobile');

  } else {
    console.log("PC端打开6666666666");
    localStorage.setItem("agennt",'PC');
  }
  // if(to.meta.content) {     //路由发生变化时候修改meta中的content
  //   let head = document.getElementByTagName('head')
  //   let meta = document.createElemnet('meta')
  //   document.querySelector('meta[name="keywords"]').setAttribute('content',to.meta.content.keywords)
  //   document.querySelector('meta[name="description"]').setAttribute('content',to.meta.content.description)
  //   meta.content = to.meta.content
  //   dead[0].appendChild(meta)
    
  // }
  // if(to.meta.title) {   //路由发生变化时候修改页面中的title
  //     document.title = to.meta.title
  // }

  // let jmData = JSON.parse(sessionStorage.getItem('jmData')) || {};
  // if (jmData.id === '10001') {
  //   console.log(to, 'to')
  //   if (to.path === '/home') return false
  // }


  // const last_way = to.matched.slice(-1)[0];
  // const reg_query_invalid = last_way.regex.keys.some(v => {
  //   if (v.pattern === '[^\\/]+?') return;//vue-router , 定义时不使用正则验证时,默认值
  //   const value_reg = new RegExp(`^(${v.pattern})$`), value = to.query[v.name];
  //   if (value && !value_reg.test(value)) return true;
  // });
  //不符合路由设置的正则，前往404页面
  // if (reg_query_invalid) return next({ path: '/404', replace: true });
  next();
});
router.afterEach((to, from) => {
  vue_state.use_loading = false;  //清除loading
  vue_state.use_immersion_nav = true; //恢复沉浸
});
export default router;
//验证query正则
test_use_reg_query.path_reg = /\:\w+\(.*?\)\???/g;
test_use_reg_query.pick_key_reg = /\:(.*?)\(/;
test_use_reg_query.pick_value_reg = /\((.*?)\)/;//路由正则是需要写在括号之内的

function test_use_reg_query (router_query, router_path) {
  const use_reg_query = Array.prototype.slice.call(router_path.match(test_use_reg_query.path_reg));
  use_reg_query.map(v => {
    const key = v.match(test_use_reg_query.pick_key_reg)[1];
    const value_reg = new RegExp(v.match(test_use_reg_query.pick_value_reg)[1]);
  });
}
// 解决跳转到当前路由报错问题
const originalPush = vue_router.prototype.push
vue_router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
